using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace AnimatedSprites
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    class Guard : Sprite
    {
        //public int visionLength = 200;
        private int walkTime=0;
        public Vector2 nextStep;
        public int acclerate = 1;
        private int frameRate = 0;
        private Texture2D vision;
        private Point visionSize;
        private Vector2[] speedVector;
       // private bool shouldTurn = false;

        Player p1;
        Player p2;

        public override Vector2 direction
        {
            get { return speed; }
        }

        public Rectangle guardCollision()
        {
                return new Rectangle(
                    (int)(logicalPosition.X),
                    (int)(logicalPosition.Y),
                    (int)((frameSize.X+(this.visionSize.X*scale))),
                    (int)((frameSize.Y+(this.visionSize.Y*scale))));
        }

        public Guard(Texture2D textureImage, Texture2D vision, Point visionSize, Vector2 logicalPosition, Point frameSize,
            int collisionOffset, Vector2 speed,
            string collisionCueName, int scoreValue, InGameManager IGM)
            : base(textureImage, logicalPosition, frameSize,
            collisionOffset, speed,
            collisionCueName, scoreValue, IGM)
        {
            p1 = IGM.p1;
            p2 = IGM.p2;
            nextStep = speed;
            this.vision = vision;
            this.visionSize = visionSize;

            speedVector = new Vector2[101];
            for (int i = 0; i < 11; i++)
            {
                speedVector[i] = new Vector2(i, 0);
            }

            for (int j = 11; j < 40; j++)
                speedVector[j] = new Vector2(10, 0);

            for (int k = 40; k < 61; k++)
                speedVector[k] = new Vector2((k-50)*(-1), 0);

            for (int l = 61; l < 91; l++)
                speedVector[l] = new Vector2(-10, 0);

            for (int m = 90; m < 101; m++)
                speedVector[m] = new Vector2(m-90-10, 0);
        }

        //return 3 if collide with both p1 and p2, 2 if collide with p2, 1 for pl, and 0 for not collide. 
        public int collide()
        {
            if (this.guardCollision().Intersects(p1.collisionRect) && this.guardCollision().Intersects(p2.collisionRect))
                return 3;
            else if (this.guardCollision().Intersects(p1.collisionRect))
                return 1;
            else if (this.guardCollision().Intersects(p2.collisionRect))
                return 2;
            else
                return 0;
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update()
        {

            frameRate++;
            if (frameRate>5)
            {
                frameRate = 0;

                walkTime++;
                if (walkTime == 101)
                    walkTime = 0;       
        
                //if (walkTime == 3 || walkTime == 11)
                //    acclerate = 0;
                //if (walkTime == 7)
                //    acclerate = -1;
                //if (walkTime == 15)
                //    acclerate = 1;

                //speed = speed + new Vector2(acclerate, 0);

                speed = speedVector[walkTime];

                nextStep = logicalPosition + speed;

                if (nextStep.X > this.spriteManager.midPoint.X+462)
                    nextStep.X = 0;
                if (nextStep.Y > this.spriteManager.midPoint.Y+334)
                    nextStep.Y = 0;

                logicalPosition = nextStep;
                
            }
            if (this.collide() != 0)
                this.spriteManager.game1.setState(5);

            base.Update();
           
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch, Vector2 midPoint, bool chosen)
        {
            viewPosition = logicalPosition - (midPoint - offset);
            if (speed.X <= 0)
            {
                spriteBatch.Draw(this.textureImage,
                    viewPosition,
                    new Rectangle(0,
                        0,
                        frameSize.X, frameSize.Y),
                    chosen ? Color.White : Color.Orange, 0, Vector2.Zero,
                    scale, SpriteEffects.FlipHorizontally, 0);

                spriteBatch.Draw(this.vision, new Vector2(viewPosition.X-visionSize.X, viewPosition.Y),
                    new Rectangle(0, 0, visionSize.X, visionSize.Y),
                    chosen ? Color.White : Color.Orange, 0, Vector2.Zero,
                    scale, SpriteEffects.FlipHorizontally, 0.01f);
            }
            else
            {
                spriteBatch.Draw(this.textureImage,
                    viewPosition,
                    new Rectangle(0,
                        0,
                        frameSize.X, frameSize.Y),
                    chosen ? Color.White : Color.Orange, 0, Vector2.Zero,
                    scale, SpriteEffects.None, 0);

                spriteBatch.Draw(this.vision, viewPosition + new Vector2(this.frameSize.X, 0),
                    new Rectangle(0, 0, visionSize.X, visionSize.Y),
                    chosen ? Color.White : Color.Orange, 0, Vector2.Zero,
                    scale, SpriteEffects.None, 0.01f);
            }
        }

    }
}
